<template>
  <div class='container'>
    <el-card>

        <el-form  :model="form" label-width="80px">
           <el-form-item  label="学科名称">
              <el-input v-model="form.value" style="width:190px;height:32px"></el-input>
               <el-button size="medium" style="margin-left:10px;font-size:12px" @click="remove">清除</el-button>
                <el-button size="medium" type="primary" style="font-size:12px" @click="search">搜索</el-button>
                <el-button @click="addDialog=true" type="success" icon="el-icon-edit" style="width:97px ;font-size:12px;margin-left:770px">新增学科</el-button>
            </el-form-item>
        </el-form>

  <template>
     <el-alert
        :title="`数据一共${row}条`"
        type="info"
        show-icon>
        </el-alert>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="序号"
        width="48">
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="学科名称"
        width="144">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者"
         width="141">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期"
         width="160">
          <template slot-scope="{row}">
            {{row.addDate|parseTime}}
         </template>
      </el-table-column>
      <el-table-column
        prop="isFrontDisplay"
        label="前台是否显示"
         width="141">
          <template slot-scope="{row}">
          {{ row.isFrontDisplay?'是':'否'}}
         </template>

      </el-table-column>
      <el-table-column
        prop="twoLevelDirectory"
        label="二级目录"
         width="141">
      </el-table-column>
      <el-table-column
        prop="tags"
        label="标签"
         width="141">
      </el-table-column>
      <el-table-column
        prop="totals"
        label="题目数量"
         width="141">
      </el-table-column>
      <el-table-column
        prop="edit"
        label="操作"
         width="240">
         <template slot-scope="{row}">
          <el-button type="text" @click="$router.push('tags')">学科分类</el-button>
         <el-button type="text" @click="$router.push('directorys')">学科标签</el-button>
         <el-button type="text" @click="edit(row)">修改</el-button>
         <el-button type="text" @click="delSub(row.id)">删除</el-button>
         </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end">
 <el-pagination
      background
      layout="prev, pager, next,sizes,jumper"
      @current-change="currentChange"
      @size-change="sizeChange"
      :page-size.sync="page.size"
      :current-page.sync="page.page"
      :total="page.total">
    </el-pagination>
    </el-row>

  </template>
    </el-card>

<!-- 新增学科弹层 -->
<el-dialog
  title="新增学科"
  :visible.sync="addDialog"
  width="400px">
    <el-form ref="addForm" :rules="rules" :model="formData"  label-width="80px">
      <el-form-item label="学科名称" prop="subjectName">
        <el-input v-model="formData.subjectName"></el-input>
      </el-form-item>
      <el-form-item label="是否开启">
       <el-switch v-model="formData.isFrontDisplay" :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="end">
      <el-button @click="closeDialog">取 消</el-button>
    <el-button type="primary" @click="btnOk">确 定</el-button>
    </el-row>

  </el-dialog>

  <!-- 修改学科弹层 -->
  <el-dialog
    title="修改学科"
    :visible.sync="updateDailog"
    width="400px">
     <el-form ref="editForm" :rules="rules" :model="formData"  label-width="80px">
      <el-form-item label="学科名称" prop="subjectName">
        <el-input v-model="formData.subjectName"></el-input>
      </el-form-item>
      <el-form-item label="是否开启">
       <el-switch v-model="formData.isFrontDisplay" :active-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
      </el-form-item>
    </el-form>
        <el-row type="flex" justify="end">
           <el-button @click="closeUpdateDailog">取 消</el-button>
            <el-button type="primary" @click="updateOk">确 定</el-button>
          </el-row>

  </el-dialog>

  </div>
</template>

<script>
import { list, add, remove, update } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      form: {
        value: ''
      },
      tableData: [],
      simpleList: [],
      array: [],
      row: '',
      page: {
        page: 1, // 当前所在的页数
        size: 10, // 每页的条数
        total: 0 // 总的数据条数
      },
      addDialog: false,
      updateDailog: false,
      formData: {
        subjectName: '',
        isFrontDisplay: 0,
        id: null
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }

    }
  },
  created () {
    this.getList()
  },
  methods: {
    edit (row) {
      this.formData = row
      this.updateDailog = true
    },
    // 搜索功能
    async search () {
      console.log(this.form.value)
      console.log(this.tableData)
      this.tableData = this.array.filter(item => {
        if (item.subjectName.indexOf(this.form.value) !== -1) {
          return item
        }
      })
      console.log(this.tableData)
    },

    async currentChange (newPage) {
      this.page.page = newPage
      const res = await list(this.page)
      this.tableData = res.data.items
      // console.log(res)
    },
    async sizeChange (newPage) {
      this.page.size = newPage
      const res = await list(this.page.size)
      this.tableData = res.data.items
    },

    // 删除功能
    async delSub (id) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await remove(id)
        this.$message.success('删除学科成功')
        this.getList()
      } catch (err) {
        console.log(err)
      }
    },

    // 修改学科功能
    async updateOk () {
      try {
        await this.$refs.editForm.validate()
        await update(this.formData)
        this.$message.success('修改学科成功')
        this.getList()
        this.closeUpdateDailog()
      } catch (err) {
        console.log(err)
      }
    },

    // 添加学科功能
    async btnOk () {
      try {
        await this.$refs.addForm.validate()
        await add(this.formData)
        this.$message.success('新增学科成功')
        this.getList()
        this.closeDialog()
      } catch (err) {
        console.log(err, 2)
      }
    },

    // 关闭修改弹窗
    closeUpdateDailog () {
      this.$refs.editForm.resetFields()
      this.updateDailog = false
    },
    // 关闭添加弹窗
    closeDialog () {
      this.$refs.addForm.resetFields()
      this.addDialog = false
    },
    // 获取学科列表
    async getList () {
      const { data } = await list()
      this.tableData = data.items
      this.array = data.items
      this.row = data.counts
      this.page.total = data.counts
      console.log(data)
    },
    remove () {
      this.form.value = ''
    }
  }
}
</script>

<style scoped lang='less'>

</style>
